import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
import util.Constants;
import util.FileUtils;
import util.HttpUtil;
import util.ZipUtils;

import java.io.File;

/**
 * 代码方式引入 https://gitee.com/allen056/android_base_library
 */
public class ImportBaseLibrary extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
        Project project = anActionEvent.getData(PlatformDataKeys.PROJECT);
        try {
            if (new File(project.getBasePath() + "/baselibrary").exists()) {
                Messages.showErrorDialog(project, "目录baselibrary已存在", "提示");
                return;
            }
            File zipFile = HttpUtil.downloadFile(Constants.BaseLibraryCode, project.getBasePath());
            ZipUtils.unzip(zipFile, project.getBasePath());
            FileUtils.deleteFile(zipFile);
            FileUtils.addToFile("\ninclude ':baselibrary'", project.getBasePath(), "settings.gradle");
            project.getBaseDir().refresh(false, true);
            Messages.showInfoMessage(project, "导入成功", "提示");
        } catch (Exception e) {
            Messages.showErrorDialog(project, "导入失败", "提示");
        }
    }
}
